<?php


namespace app\logic\traits;
/**
 * 阿里云oss
 * Trait OssClient
 * @package app\logic\traits
 */

use OSS\Credentials\StaticCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
use app\admin\controller\Common;
trait AliyunOss
{
    protected $ossClient;
    protected $accessKeyId='LTAI5tAbTsiWDqNouv4NDeQv';
    protected $accessKeySecret='M2prWKwea40YdQDrY7oLHfodh8pGiF';
    protected $endpoint='jcd-music.oss-cn-wulanchabu.aliyuncs.com';
    protected $url='';


    /**
     * 文件上传
     * @param $bucket string 桶名称
     * @param $object string 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
     * @param $filePath string 由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
     * @return array
     */
    function uploads($filePath,$object='',$bucket='jcd-music'){
        $this->setsssEndpoint($bucket);
        $ossClient=new OssClient($this->accessKeyId,$this->accessKeySecret,$this->endpoint,true);
        try {
            $res=$ossClient->uploadFile($bucket, $object, $filePath);
            return ['code'=>1,'msg'=>$this->url.$object];
        }catch (OssException $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }

    }

    
    private function setsssEndpoint($bucket='jcd-music'){
        $this->endpoint='jcd-music.oss-cn-wulanchabu.aliyuncs.com';
        $this->url='https://jcd-music.oss-cn-wulanchabu.aliyuncs.com/';
    }

    public function getProvider(){
        $provider = new StaticCredentialsProvider('LTAI5tBPmnmD58RCrgrHtdRy', '1yweA2nVlaBdbwmdtwaisiLyhlHU27');
        dump($provider);die;
    }

}